
# DynamicObject.rb
# Abstract class to define a generic object in the game.

require 'libs/gosu'

class DynamicObject

  attr_reader :pos_x, :pos_y, :vel_x, :vel_y, :angle, :size

  def initialize(holder, options={})
    @size   = options['size']
    @pos_x  = @pos_y = @vel_x = @vel_y = @angle = 0.0
    @alive  = true
  end

  # Set a position and an angle for the object.
  def place(x, y, angle=0)
    @pos_x, @pos_y, @angle = x, y, angle
  end

  # Tell if an object is in the game.
  def out?
    (@pos_x < 0 or @pos_x > 800) or (@pos_y < 0 or @pos_y > 600)
  end

  # Tell if the object has been hit by another.
  def collision?(object)
    Gosu::distance(@pos_x, @pos_y, object.pos_x, object.pos_y) < object.size
  end

  # Perform this action when the object is no longer necesary.
  def destroy
    @pos_x = @pos_y = 1000
  end

  # Move the object to its next position (needed by Gosu::Window).
  def move
    condition = self.out?

    @pos_x += @vel_x unless condition
    @pos_y += @vel_y unless condition
  end

  # Draw the object in its next position (needed by Gosu::Window).
  def draw
    @image.draw_rot(@pos_x, @pos_y, 1, @angle)
  end

end
